package zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.maritalstatus.MaritalStatusInfo;
import zzs.generalframework.common.core.parameter.commonparameter.maritalstatus.MaritalStatusPO;
import zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.dao.IMaritalStatusDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.dao.impl.maritalStatusDaoImpl")
public class MaritalStatusDaoImpl implements IMaritalStatusDao
{
	private Logger logger = LoggerFactory.getLogger(MaritalStatusDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(MaritalStatusPO maritalStatusPO)
	{
		logger.info("====插入婚姻状况数据开始====");
		logger.debug("====插入婚姻状况数据,入参:MaritalStatusPO=[{}]====", maritalStatusPO);
		entityManager.persist(maritalStatusPO);
		logger.debug("====插入婚姻状况数据,出参:boolean=[{}]====", true);
		logger.info("====插入婚姻状况数据结束====");
		return true;
	}

	@Override
	public boolean update(MaritalStatusPO maritalStatusPO)
	{
		logger.info("====更新婚姻状况数据开始====");
		logger.debug("====更新婚姻状况数据,入参:MaritalStatusPO=[{}]====", maritalStatusPO);
		entityManager.merge(maritalStatusPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新婚姻状况数据结束====");
		return true;
	}

	@Override
	public List<MaritalStatusPO> select(MaritalStatusInfo maritalStatusInfo)
	{
		logger.info("====查询婚姻状况数据开始====");
		logger.debug("====查询婚姻状况数据,入参:MaritalStatusInfo=[{}]====", maritalStatusInfo);
		List<MaritalStatusPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from MaritalStatusPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(maritalStatusInfo.getMaritalStatusID()))
		{
			sqlBuffer.append(" and a.maritalStatusID=?");
			parameter.putParameterObject(maritalStatusInfo.getMaritalStatusID());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusInfo.getMaritalStatusCode()))
		{
			sqlBuffer.append(" and a.maritalStatusCode=?");
			parameter.putParameterObject(maritalStatusInfo.getMaritalStatusCode());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusInfo.getMaritalStatusName()))
		{
			sqlBuffer.append(" and a.maritalStatusName=?");
			parameter.putParameterObject(maritalStatusInfo.getMaritalStatusName());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(maritalStatusInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(maritalStatusInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询婚姻状况数据,出参:List<MaritalStatusPO>=[{}]====", returnList);
		logger.info("====查询婚姻状况数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(MaritalStatusPO maritalStatusPO)
	{
		logger.info("====删除婚姻状况数据开始====");
		logger.debug("====删除婚姻状况数据,入参:MaritalStatusPO=[{}]====", maritalStatusPO);
		entityManager.remove(maritalStatusPO);
		logger.debug("====删除婚姻状况数据,出参:boolean=[{}]====", true);
		logger.info("====删除婚姻状况数据结束====");
		return true;
	}

	@Override
	public Long count(MaritalStatusPO maritalStatusPO)
	{
		logger.info("====统计婚姻状况数据开始====");
		logger.debug("====统计婚姻状况数据,入参:MaritalStatusPO=[{}]====", maritalStatusPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from MaritalStatusPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(maritalStatusPO.getMaritalStatusID()))
		{
			sqlBuffer.append(" and a.maritalStatusID=?");
			parameter.putParameterObject(maritalStatusPO.getMaritalStatusID());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusPO.getMaritalStatusCode()))
		{
			sqlBuffer.append(" and a.maritalStatusCode=?");
			parameter.putParameterObject(maritalStatusPO.getMaritalStatusCode());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusPO.getMaritalStatusName()))
		{
			sqlBuffer.append(" and a.maritalStatusName=?");
			parameter.putParameterObject(maritalStatusPO.getMaritalStatusName());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusPO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(maritalStatusPO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(maritalStatusPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(maritalStatusPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计婚姻状况数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计婚姻状况数据结束====");
		return returnLong;
	}

}
